import { defineStore } from 'pinia'
import { 
  getUnreadCount, 
  getNotificationList, 
  markAsRead, 
  markAllAsRead, 
  deleteNotification 
} from '@/api/notification'

export const useNotificationStore = defineStore('notification', {
  state: () => ({
    unreadCount: 0,
    notifications: [],
    loading: false,
    totalCount: 0,
    currentPage: 1,
    pageSize: 10,
  }),
  
  getters: {
    hasUnread: (state) => state.unreadCount > 0,
  },
  
  actions: {
    // 获取未读通知数量
    async fetchUnreadCount() {
      try {
        const res = await getUnreadCount()
        if (res.code === 200) {
          this.unreadCount = res.data
        }
      } catch (error) {
        console.error('获取未读通知数量失败', error)
      }
    },
    
    // 获取通知列表
    async fetchNotifications(params = {}) {
      try {
        this.loading = true
        const page = params.pageNum || this.currentPage
        const size = params.pageSize || this.pageSize
        const isRead = params.isRead !== undefined ? params.isRead : null
        
        const res = await getNotificationList({
          pageNum: page,
          pageSize: size,
          isRead
        })
        
        if (res.code === 200) {
          this.notifications = res.data.records
          this.totalCount = res.data.total
          this.currentPage = res.data.pageNum
          this.pageSize = res.data.pageSize
        }
      } catch (error) {
        console.error('获取通知列表失败', error)
      } finally {
        this.loading = false
      }
    },
    
    // 标记通知为已读
    async readNotification(notificationId) {
      try {
        const res = await markAsRead(notificationId)
        if (res.code === 200) {
          // 更新本地通知状态
          const notification = this.notifications.find(n => n.id === notificationId)
          if (notification) {
            notification.isRead = 1
          }
          // 更新未读数量
          if (this.unreadCount > 0) {
            this.unreadCount--
          }
          return true
        }
        return false
      } catch (error) {
        console.error('标记通知已读失败', error)
        return false
      }
    },
    
    // 标记所有通知为已读
    async readAllNotifications() {
      try {
        const res = await markAllAsRead()
        if (res.code === 200) {
          // 更新所有本地通知状态
          this.notifications.forEach(notification => {
            notification.isRead = 1
          })
          // 重置未读数量
          this.unreadCount = 0
          return true
        }
        return false
      } catch (error) {
        console.error('标记所有通知已读失败', error)
        return false
      }
    },
    
    // 删除通知
    async removeNotification(notificationId) {
      try {
        const res = await deleteNotification(notificationId)
        if (res.code === 200) {
          // 从列表中移除
          this.notifications = this.notifications.filter(n => n.id !== notificationId)
          return true
        }
        return false
      } catch (error) {
        console.error('删除通知失败', error)
        return false
      }
    },
    
    // 重置状态
    resetState() {
      this.unreadCount = 0
      this.notifications = []
      this.totalCount = 0
      this.currentPage = 1
    }
  }
}) 